package online.yysh.charging.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;

import online.yysh.charging.R;
import online.yysh.charging.base.BaseFragment;
import online.yysh.charging.bean.User;
import online.yysh.charging.bean.response.GetAuthMsgResponse;
import online.yysh.charging.utils.HttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 未完成订单
 */

public class IncompleteFragment extends BaseFragment implements View.OnClickListener {

    private TextView tv_no,tv_time,tv_device_id,tv_quantity,tv_use_money,tv_other_money,tv_money;

    private User user;
    //查询用户状态信息成功
    private static final int QUERY_USER_SUCCEED = 600;
    //查询用户状态信息失败
    private static final int QUERY_USER_FAILED = 601;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case QUERY_USER_SUCCEED:
                    if (3 == user.getStatus()){
                        tv_device_id.setText("设备ID："+user.getDeviceId()+"-枪号："+user.getPort());
                        tv_time.setText("开始时间："+user.getStartTime());
                        tv_other_money.setText("充电时长(时)："+user.getTimeCharged());
                        tv_quantity.setText("充电量(kwh)："+user.getUseKWh());
                        tv_money.setText("充电进度："+user.getSOC()+"%");
                        tv_use_money.setText("充电金额(元)："+user.getUserMoney());
                    }else{
                        showToast("无正在进行中的订单");
                    }
                    break;
                case QUERY_USER_FAILED:
                    showToast("查询订单失败");
                    break;
            }
        }
    };

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View itemView = inflater.inflate(R.layout.fragment_incomplete, null);
        tv_no = itemView.findViewById(R.id.tv_no);
        tv_time = itemView.findViewById(R.id.tv_time);
        tv_device_id = itemView.findViewById(R.id.tv_device_id);
        tv_quantity = itemView.findViewById(R.id.tv_quantity);
        tv_use_money = itemView.findViewById(R.id.tv_use_money);
        tv_other_money = itemView.findViewById(R.id.tv_other_money);
        tv_money = itemView.findViewById(R.id.tv_money);
        return itemView;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        checkChargingStatus();
    }

    @Override
    public void onClick(View view) {

    }

    /**
     * 检测当前账户充电状态
     */
    private void checkChargingStatus() {
        if (!TextUtils.isEmpty(openid)) {
            HttpUtil.getInstance().getAuthMsg(openid, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(QUERY_USER_FAILED);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();
                    Log.e(TAG, "getAuthMsg: " + result);
                    GetAuthMsgResponse data = new Gson().fromJson(result, GetAuthMsgResponse.class);
                    if ("ok".equals(data.getStatus())) {
                        user = data.getData();
                        handler.sendEmptyMessage(QUERY_USER_SUCCEED);
                    } else {
                        //获取信息失败
                        handler.sendEmptyMessage(QUERY_USER_FAILED);
                    }
                }
            });
        }
    }
}
